package com.lzl.eduservice.controller;


import com.lzl.common_utils.R;
import com.lzl.eduservice.entity.EduCourse;
import com.lzl.eduservice.entity.vo.CourseInfoVo;
import com.lzl.eduservice.entity.vo.CoursePublishVo;
import com.lzl.eduservice.service.EduCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-03-30
 */
@RestController
@RequestMapping("/eduservice/course")
public class EduCourseController {

    @Autowired
    private EduCourseService eduCourseService;


    /**
     * 添加课程基本信息的方法
     * @param courseInfoVo
     * @return
     */
    @PostMapping("addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        //需要返回课程id，为了后面的添加大纲使用
        String id = eduCourseService.saveCourseInfo(courseInfoVo);

        return R.ok().data("courseId",id);
    }

    /**
     * 根据课程id查询课程基本信息
     * 数据回显--需要返回数据有课程简介---所以用他们直接的VO类-CourseInfoVo
     * @param courseId
     * @return
     */
    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId) {
        CourseInfoVo courseInfoVo = eduCourseService.getCourseInfo(courseId);
        return R.ok().data("courseInfoVo",courseInfoVo);
    }



    //修改课程信息
    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        eduCourseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }

    //根据课程 id 值 查询课程确认信息
    @GetMapping("getPublishCourseInfo/{courseId}")
    public R getPublishCourseInfo(@PathVariable String courseId){
        CoursePublishVo coursePublishVo = eduCourseService.publishCourseInfo(courseId);
        return R.ok().data("publishCourse",coursePublishVo);
    }

    /**
     * 课程最终提交
     * @param courseId
     * @return
     */
    @PostMapping("publishCourse/{courseId}")
    public R publishCourse(@PathVariable String courseId){
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(courseId);
        eduCourse.setStatus("Normal"); //设置课程发布状态
        eduCourseService.updateById(eduCourse);
        return R.ok();

    }

    /**
     * 课程列表
     * @return
     */
    @GetMapping("getCourseList")
    public R getCourseList(){
        List<EduCourse> courseList = eduCourseService.list(null);
        return R.ok().data("courseList",courseList);
    }

    /**
     * 删除课程
     * @param courseId
     * @return
     */
    @DeleteMapping("deleteCourse/{courseId}")
    public R deleteCourse(@PathVariable String courseId){

        eduCourseService.removeCourse(courseId);
        return R.ok();
    }


}

